define(function() {
	"use strict";
	/**
		表单上传辅助类, 用于提供表单上传的规范方法与接口.依赖jQuery
		@example
		var formUploader = new FormUploader(formElement, config);
		var promise = formUploader.submit();
		promise.then(function(data){ //todo:应用回调函数代码 })

		@class FormUploader
		@param {object} [config] 配置信息
		@param {object} [config.namespace] 表单提交地址
		@param {object} [config.action] 表单提交地址
		@param {object} [config.params] 表单提交地址后额外的查询参数
		@param {object} [config.context=window] 回调函数执行时的this指向的对象
		@param {string} [config.callbackQueryParam=callback] 传给被访问应用的回调函数参数名,取代最终url中的"callback="中的callback
		@param {string} [config.callbackName] 回调函数名,取代最终url中的"callback=formUploadCallback"中的formUploadCallback
	 */
	function FormUploader(formElement, config){
		if( !(formElement instanceof HTMLFormElement) ){
			return;
		}
		if(formElement._formUploader){
			formElement._formUploader.destory();
		}
		formElement._formUploader = this;
		this.formElement = formElement;
		this.config = config;

		this.setTarget();
	}
	/**
		链接指定应用的指定路由
		@memberof FormUploader
	*/
	FormUploader.prototype.submit = function(){
		var config = this.config;
		var deferred = this.deferred = jQuery.Deferred();
		
		this.callbackName = config.callbackName || this.buildCallbackName();

		this.createCallback();

		this.setAction();

		this.formElement.submit();

		return deferred.promise();
	}

	/**
		设置表单target
		@memberof FormUploader
	*/
	FormUploader.prototype.setTarget = function(){
		var targetName = "formUploadTargetIframe" + (+new Date);
		var $ifr = $('<iframe src="about:blank" style="display:none;"></iframe>');

		$ifr.attr("name", targetName);
		$ifr.appendTo(this.formElement);

		this._$ifr = $ifr;

		this.formElement._targetIframe = targetName;
		this.formElement.target = targetName;
	}
	/**
		生成回调函数名
		@memberof FormUploader
	*/
	FormUploader.prototype.buildCallbackName = function(){
		var callbackName = "formUploadCallback" + (+new Date);
		if( isLocalDebugModel ){
			callbackName = "formUploadCallback";
		}
		return callbackName;
	}
	/**
		设置表单action
		@memberof FormUploader
	*/
	FormUploader.prototype.setAction = function(){
		var config = this.config,
			action = config.action,
			params = config.params||{},
			searchStr="";

		params = jQuery.extend({}, params);
		params[params.callbackQueryParam || "callback"] = "parent."+this.callbackName;
		for(var key in params){
			if(params.hasOwnProperty(key)){
				searchStr += "&" + key + "=" + encodeURIComponent(params[key]);
			}
		}
		//用于开发
		if( isLocalDebugModel ){
			action = action.replace(/\//g,'_');
			action = action.replace('_','/');
			action += '.html';
		}
		this.formElement.action = action+'?' + searchStr.substr(1);
	}
	/**
		生成全局回调函数
		@memberof FormUploader
	*/
	FormUploader.prototype.createCallback = function(){
		var config = this.config,
			deferred = this.deferred,
			formUploader = this;
		window[this.callbackName] = function(response) {
			if(response.status == "success"){
				deferred.resolveWith(config.context || this, [Ember.copy(response,true)]);
			}else{
				deferred.rejectWith(config.context || this, [Ember.copy(response,true)]);
			}
			formUploader.destoryCallback();
		};
	}
	/**
		销毁全局回调函数
		@memberof FormUploader
	*/
	FormUploader.prototype.destoryCallback = function(){
		delete window[this.callbackName];
	}
	/**
		销毁全局回调函数
		@memberof FormUploader
	*/
	FormUploader.prototype.destory = function(){
		this.destoryCallback();
		this.formElement._formUploader = null;
		this.formElement = null;
		this._$ifr.remove();
		this._$ifr = null;
	}
	return FormUploader;
});